package com.yanfan.zutai.controller;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.yanfan.common.core.controller.BaseController;
import com.yanfan.common.core.domain.AjaxResult;
import com.yanfan.common.core.domain.model.LoginUser;
import com.yanfan.common.core.page.TableDataInfo;
import com.yanfan.common.utils.SecurityUtils;
import com.yanfan.common.utils.poi.ExcelUtil;
import com.yanfan.zutai.domain.BEchartZt;
import com.yanfan.zutai.service.IBEchartZtService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 大屏管理Controller
 *
 * @author ruoyi
 * @date 2023-05-24
 */
@RestController
@RequestMapping("/zutai/bEchartZt")
public class BEchartZtController extends BaseController {
    @Autowired
    private IBEchartZtService bEchartZtService;
    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * 查询大屏管理列表
     */
    @GetMapping("/list")
    public TableDataInfo list(BEchartZt bEchartZt) {
        startPage();
        List<BEchartZt> list = bEchartZtService.selectBEchartZtList(bEchartZt);
        return getDataTable(list);
    }

    /**
     * 导出大屏管理列表
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, BEchartZt bEchartZt) {
        List<BEchartZt> list = bEchartZtService.selectBEchartZtList(bEchartZt);
        ExcelUtil<BEchartZt> util = new ExcelUtil<BEchartZt>(BEchartZt.class);
        util.exportExcel(response, list, "大屏管理数据");
    }

    /**
     * 获取大屏管理详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(bEchartZtService.selectBEchartZtById(id));
    }

    /**
     * 新增大屏管理
     */
    @PostMapping
    public AjaxResult add(@RequestBody BEchartZt bEchartZt) {
        return toAjax(bEchartZtService.insertBEchartZt(bEchartZt));
    }

    /**
     * 修改大屏管理
     */
    @PutMapping
    public AjaxResult edit(@RequestBody BEchartZt bEchartZt) {
        return toAjax(bEchartZtService.updateBEchartZt(bEchartZt));
    }

    /**
     * 删除大屏管理
     */
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(bEchartZtService.deleteBEchartZtByIds(ids));
    }

    @GetMapping("/getZtData")
    public AjaxResult getZtData(BEchartZt bEchartZt) {
        if (StrUtil.isEmpty(bEchartZt.getGuid())) {
            return AjaxResult.error("系统中无此组态界面！");
        }
        BeanPropertyRowMapper<BEchartZt> bDeviceDetailMapper = new BeanPropertyRowMapper<>(BEchartZt.class);
        String sql = "select page_name,page_data from b_echart_zt where guid=? ";
        try {
            bEchartZt = jdbcTemplate.queryForObject(sql, bDeviceDetailMapper, bEchartZt.getGuid());// 处理结果集EmptyResultDataAccessException，不做任何操作
        } catch (EmptyResultDataAccessException e) {
        }
        return AjaxResult.success(bEchartZt);
    }

    /*
     * 保存组态信息
     * */
    @PostMapping(value = "/saveZt")
    public AjaxResult saveZt(@RequestBody BEchartZt bEchartZt) {
        if (StrUtil.isEmpty(bEchartZt.getGuid())) {
            return AjaxResult.error("参数不符合规定");
        }
        BEchartZt bEchartZtQuery = new BEchartZt();
        bEchartZtQuery.setGuid(bEchartZt.getGuid());
        List<BEchartZt> bEchartZts = bEchartZtService.selectBEchartZtList(bEchartZtQuery);
        if (bEchartZts.size() > 0) {
            BEchartZt echartZt = bEchartZts.get(0);
            echartZt.setPageData(bEchartZt.getPageData());
            bEchartZtService.updateBEchartZt(echartZt);
        } else {
            bEchartZtService.insertBEchartZt(bEchartZt);
        }
        return AjaxResult.success();
    }

    /*
     * 复制组态信息
     * */
    @PostMapping(value = "/copyZtData")
    public AjaxResult copyZtData(@RequestBody BEchartZt bEchartZt) {
        String ztData = bEchartZt.getPageData();
        String guid = UUID.randomUUID().toString();
        bEchartZt.setId(null);
        bEchartZt.setGuid(guid);
        bEchartZt.setPageName(bEchartZt.getPageName() + "_副本");
        bEchartZt.setPageData(ztData);
        bEchartZtService.insertBEchartZt(bEchartZt);
        return AjaxResult.success();
    }

    @PostMapping("/importZtJson")
    public AjaxResult importZtJson(@RequestBody MultipartFile file, String deviceImei, String guid) throws Exception {
        InputStream inputStream = file.getInputStream();
        if (file.isEmpty()) {
            return AjaxResult.error("无效的配置文件");
        }
        if (file.getOriginalFilename().indexOf("json") == -1) {
            return AjaxResult.error("无效的配置文件");
        }
        BEchartZt bEchartZt = new BEchartZt();
        try {
            bEchartZt = JSON.parseObject(inputStream, BEchartZt.class);
        } catch (Exception e) {
            return AjaxResult.error("无效的配置文件");
        } finally {
            inputStream.close();
        }
        String ztData = bEchartZt.getPageData();
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (StrUtil.isEmpty(guid)) {
            guid = UUID.randomUUID().toString();
            bEchartZt.setId(null);
            bEchartZt.setGuid(guid);
            bEchartZt.setPageData(ztData);
            String deptIds = "," + loginUser.getUser().getDept().getAncestors() + "," + loginUser
                    .getUser()
                    .getDeptId() + ",";
            bEchartZt.setDeptIdStrs(deptIds);
            bEchartZt.setCreateBy(loginUser.getUsername());
            bEchartZt.setCreateTime(new Date());
            bEchartZt.setUpdateBy(loginUser.getUsername());
            bEchartZt.setUpdateTime(new Date());
        } else {
            BEchartZt bDeviceZtQuery = new BEchartZt();
            bDeviceZtQuery.setGuid(guid);
            List<BEchartZt> bDeviceZts = bEchartZtService.selectBEchartZtList(bDeviceZtQuery);
            if (bDeviceZts.size() > 0) {
                bDeviceZtQuery = bDeviceZts.get(0);
            }
            bDeviceZtQuery.setPageData(ztData);
            bEchartZtService.updateBEchartZt(bDeviceZtQuery);
        }
        return AjaxResult.success("导入成功");
    }
}
